<?php

namespace app\mp\admin;

use app\system\admin\Admin;
use think\Db;

/**
 * 网站流量统计--pv,uv,ip
 * https://blog.csdn.net/h330531987/article/details/75315006
 * 一.PV、UV、IP的英文解释
 * PV(page view)：页面访问量，每刷新一次就被记一次PV。
 * UV(Unqie Vistor)：独立访客,访问您网站的一台电脑客户端为一个访客。24小时之内，同一地址，多次访问，只算一次,一般做法是记COOKIE，但是可以伪造。
 * IP(internet protocol)：独立IP数。24小时内相同地址被计算一次。
 */
class Pvuvip extends Admin
{

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 记录pv
     * @author zhuguosen 905502314@qq.com
     * @date 2022/6/10
     */
    public function index()
    {
        $url_str = $this->request->param('url_str/s', '');
        if ($url_str == '') {
            return json(['code' => 10, 'msg' => '',]);
        }
        $url_path = pathinfo($url_str);
        $url_sort = substr($url_path['dirname'], strpos($url_path['dirname'], 'admin.php/') + 10);
        $url_sort .= "/{$url_path['filename']}";
        $m_info = db('system_menu m')->field('id,url')->where(['url' => $url_sort])->find();
        db('pvuvip')->insert([
            'url_id' => $m_info['id'] ?? 0,
            'url_sort' => $url_sort,
            'url_all' => $url_str,
            'type' => 0,
            'ip' => get_client_ip(),
            'admin_id' => defined('ADMIN_ID') ? ADMIN_ID : 0,
            'update_time' => time(),
            'create_time' => time(),
        ]);
        return json(['code' => 0, 'msg' => '',]);
    }

    /**
     * 获取每个页面的pv uv值
     * @author zhuguosen 905502314@qq.com
     * @date 2022/6/10
     */
    public function get()
    {
        $id = $this->request->param('id/d', 0);
        $url_sort = trim($this->request->param('url_sort/s', ''));
        if ($id <= 0 && $url_sort == '') {
            return json(['code' => 0, 'msg' => '', 'data' => [
                'pv' => 0,
                'uv' => 0,
            ]]);
        }
        if ($url_sort != '') {
            $m_info = db('system_menu m')->field('id,url')->where(['url' => $url_sort])->find();
        }
        if ($id > 0) {
            $m_info = db('system_menu m')->field('id,url')->where(['id' => $id])->find();
        }
        $pv = db('pvuvip')->where([
            ['url_id', '=', $m_info['id']],
//            ['url_sort', '=', $m_info['url']],
            ['create_time', '>=', mktime(0, 0, 0, date('m'), date('d'), date('Y'))],
        ])->count();
        $uv_list = db('pvuvip p')
            ->field('p.admin_id,u.username,count(0) as pv_count ')
            ->join('system_user u', 'p.admin_id=u.id', 'left')
            ->where([
                ['p.url_id', '=', $m_info['id']],
//            ['url_sort', '=', $m_info['url']],
                ['p.create_time', '>=', mktime(0, 0, 0, date('m'), date('d'), date('Y'))],
            ])->group('p.admin_id')->select();
        $this->assign('uv_list', $uv_list);
        return json(['code' => 0, 'msg' => '', 'data' => [
            'pv' => $pv,
            'uv' => count($uv_list),
            'canShowContent' => in_array(ADMIN_ROLE, [1, 3, 23]),
            'content' => in_array(ADMIN_ROLE, [1, 3, 23]) == true ? $this->fetch()->getContent() : '',
        ]]);
    }
}